package com.tdam2014.grupo3;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;

import com.tdam2014.grupo3.db.DatabaseHelper;
import com.tdam2014.grupo3.entities.People;
import com.tdam2014.grupo3.interfaces.PeopleSelectionListener;
import com.tdam2014.grupo3.utils.ImagesUtils;

public class PeopleActivity extends Activity implements PeopleSelectionListener {
	
	private PeopleFragment peopleFragment;
	
	public static final String KEY_PEOPLE_NSID = "peopleNsid";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_people);
		
		peopleFragment = (PeopleFragment) getFragmentManager().findFragmentById(R.id.peopleFragment);
		peopleFragment.setPeopleSelectionListener(this);
		
		PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
		
		// Crear la BD por si es el primer inicio de la aplicacion
		DatabaseHelper DBHelper = new DatabaseHelper(this);
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.close();
		
		// Verificar existencia de directorios
		ImagesUtils.verifyDirectoriesExistence();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_options, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.action_settings: 	
				Intent settingsIntent = new Intent(this, SettingsActivity.class);
				startActivity(settingsIntent);
				break;
			
			case R.id.exit:
				exitApplication();
				break;
		}
		
		return true;
	}
	
	public void exitApplication() {
		finish();
		System.exit(0);
	}

	@Override
	public void onPeopleSelected(People people) {
		Intent intent = new Intent(this, PhotoSetActivity.class);
		intent.putExtra(KEY_PEOPLE_NSID, people.getNsid());
		startActivity(intent);
	}
}
